﻿using System;
using System.Collections.Generic;
using System.Data;
using DBUtility;
using System.Data.SqlClient;
using Model.RF.ATETest;
using IDAL.RF.ATETest;

namespace DAL.RF.ATETest
{
    public class tb_result_DFL : BaseRepository_DT<tb_result_DFL_Info>, Itb_result_DFL
    {
        #region 静态变量定义
        private const string PARM_TB_PRODUCTSN = "@ProductSN";
        #endregion


        #region 数据检索部分
        //电性能与互调在一起
        public IList<tb_result_DFL_Info> tb_result_DFL_GetbySN(string sn)
        {
            SqlParameter parm = GetParameter();
            parm.Value = sn;
            return ExcuteSqlToGetEntities("tb_result_DFL_selectbySN", parm);
        }

        public IList<tb_result_DFL_Info> tb_result_DFL_GetbyDT(DateTime dt1, DateTime dt2)
        {
            SqlParameter[] parms = GetDTParameters();
            parms[0].Value = dt1;
            parms[1].Value = dt2;
            return ExcuteSqlToGetEntities("tb_result_DFL_selectbyDT", parms);
        }

        public IList<tb_result_DFL_Info> tb_result_DFL_GetbySN_withoutFilter(string sn)
        {
            SqlParameter parm = GetParameter();
            parm.Value = sn;
            return ExcuteSqlToGetEntities("tb_result_DFL_selectbySN_withoutFilter", parm);
        }

        public IList<tb_result_DFL_Info> tb_result_DFL_GetbyDT_withoutFilter(DateTime dt1, DateTime dt2)
        {
            SqlParameter[] parms = GetDTParameters();
            parms[0].Value = dt1;
            parms[1].Value = dt2;
            return ExcuteSqlToGetEntities("tb_result_DFL_selectbyDT_withoutFilter", parms);
        }
        #endregion

        #region 重写基类方法
        public override tb_result_DFL_Info GetEntityRecord(SqlDataReader rdr)
        {
            tb_result_DFL_Info entity = new tb_result_DFL_Info()
            {
                resultid = rdr.GetInt32(0),
                partcode = rdr.GetValue(1).ToString(),
                testtime = rdr.GetDateTime(2),
                testperiod = rdr.GetInt32(3),
                testequipid = rdr.GetValue(4).ToString(),
                projname = rdr.GetValue(5).ToString(),
                projver = rdr.GetValue(6).ToString(),
                tfpid = rdr.GetValue(7).ToString(),
                userid = rdr.GetValue(8).ToString(),
                totalresult = rdr.GetValue(9).ToString(),
                itemresult = rdr.GetValue(10).ToString(),
                itemselinfo = rdr.GetValue(11).ToString(),
                itemselcnt = rdr.GetInt32(12),
                itemcnt = rdr.GetInt32(13),
                remark = rdr.GetValue(14).ToString()
            };
            return entity;
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter parm = new SqlParameter(PARM_TB_PRODUCTSN, SqlDbType.VarChar, 50);
            return parm;
        }
        #endregion


        //----------------------------------------------------------------------------------------------------------------------

        #region 用于WebService的中兴格式数据录入
        private const string PARM_TB_PARTCODE = "@partcode";
        private const string PARM_TB_TESTTIME = "@testtime";
        private const string PARM_TB_TESTPERIOD = "@testperiod";
        private const string PARM_TB_TESTEQUIPID = "@testequipid";
        private const string PARM_TB_TFPID = "@tfpid";
        private const string PARM_TB_USERID = "@userid";
        private const string PARM_TB_TOTALRESULT = "@totalresult";
        private const string PARM_TB_ITEMRSULT = "@itemresult";
        private const string PARM_TB_ITEMSELINFO = "@itemselinfo";
        private const string PARM_TB_ITEMSELCNT = "@itemselcnt";
        private const string PARM_TB_ITEMCNT = "@itemcnt";
        private const string PARM_TB_REMARK = "@remark";
        private const string PARM_TB_ISPIM = "@IsPIM";
        private const string PARM_TB_PRODUCTTYPEID = "@ProductTypeID";

        private const string PARM_TB_DT = "@TestItemdt";

        public bool tb_ATETestAndItem_Insert(tb_result_DFL_Info ti, string productTypeID, DataTable itemdt)
        {
            List<SqlParameter> ItemList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_TB_DT,itemdt)};
            SqlParameter[] Parms = GetParameters();
            SetParameters(ti, productTypeID, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in ItemList)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                try
                {
                    cmd.CommandText = "tb_ATETestAndItem_Insert";
                    using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_TB_PARTCODE, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_TB_TESTTIME,SqlDbType.DateTime,8),
					new SqlParameter(PARM_TB_TESTPERIOD, SqlDbType.Int, 4),
                    new SqlParameter(PARM_TB_TESTEQUIPID, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_TB_TFPID, SqlDbType.NVarChar, 60),
                    new SqlParameter(PARM_TB_USERID,SqlDbType.NVarChar,50),
					new SqlParameter(PARM_TB_TOTALRESULT, SqlDbType.NVarChar, 2),
                    new SqlParameter(PARM_TB_ITEMRSULT, SqlDbType.NVarChar, 500),
					new SqlParameter(PARM_TB_ITEMSELINFO, SqlDbType.NVarChar, 500),
                    new SqlParameter(PARM_TB_ITEMSELCNT,SqlDbType.Int ,4),
					new SqlParameter(PARM_TB_ITEMCNT, SqlDbType.Int , 4),
                    new SqlParameter(PARM_TB_REMARK, SqlDbType.NVarChar, 100),
                    new SqlParameter(PARM_TB_ISPIM, SqlDbType.Bit, 1),
                    new SqlParameter(PARM_TB_PRODUCTTYPEID, SqlDbType.NVarChar, 50)
                };
            return parms;
        }

        public void SetParameters(tb_result_DFL_Info ti,string productTypeID, params SqlParameter[] Parms)
        {
            Parms[0].Value = ti.partcode;
            Parms[1].Value = ti.testtime;
            Parms[2].Value = ti.testperiod;
            Parms[3].Value = ti.testequipid;
            Parms[4].Value = ti.tfpid;
            Parms[5].Value = ti.userid;
            Parms[6].Value = ti.totalresult;
            Parms[7].Value = ti.itemresult;
            Parms[8].Value = ti.itemselinfo;
            Parms[9].Value = ti.itemselcnt;
            Parms[10].Value = ti.itemcnt;
            Parms[11].Value = ti.remark;
            Parms[12].Value = ti.IsPIM;
            Parms[13].Value = productTypeID;
        }
        #endregion
    }
}
